package domain.items;

import java.util.Date;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

import server.ForumServer;

/**
 * @author shai
 *
 */
@Searchable
public class Post extends Item {

	
	/**
	 * special Id retrieved from the MetaData, 
	 * used for the searchEngine Indexing 
	 */
	@SearchableId
	private long _postId;

	/**
	 * creator of the post
	 */
	@SearchableProperty(name = "owner")
	String _owner;

	/**
	 * creation date
	 */
	// private Calendar _date;
//	@SearchableProperty(name = "_date")
	@SearchableProperty(format = "dd/MM/yy HH:mm:ss")
	private Date _date;

	/**
	 * the post itself (also referred to as message)
	 */
	@SearchableProperty(name = "contents")
	String _Content;

	private double _score;

	/**
	 * Constructor.
	 *
	 * @param father
	 * @param owner
	 * @param subject
	 * @param content
	 */
	public Post(Item father, String owner, String subject, String content) {
		super(father);
		if(ForumServer._metaData != null){
			this._postId = ForumServer._metaData._idCounter++;
		}
		// this._date = new GregorianCalendar();
		this._date = new Date();
		this._owner = owner;
		this._type = Item.POST;
		this._Content = content;
		this._name = subject;
		this._score = -1;
	}
	

	public void setScore(double scr) {
		this._score = scr;
	}

	public double getScore() {
		return this._score;
	}

	/**
	 * private constructor, used inorder to create a clone
	 *
	 * @param father
	 */
	private Post() {
		super();
	}

	/**
	 * setters
	 */
	public boolean setContent(String newContent) {
		try {
			this._Content = newContent;
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * getters
	 */
	public String getText() {
		return this._Content;
	}

	public String getOwner() {
		// TODO Auto-generated method stub
		return this._owner;
	}

	public PostPacket convertToPacket() {
		String ownerName;
		if (this._owner == null) { // i don't know why, but it happens. also
									// can happen after we delete a user.
			ownerName = "Unknown User";
		} else {
			ownerName = this._owner;
		}
		PostPacket packet = new PostPacket(ownerName, this._name, this._Content,this._date);
		packet.setName(this._name);
		if (this._score >= 0) {
			String tScoreStr = Double.toString(this._score);
			if (tScoreStr.length() > 5) {
				tScoreStr = tScoreStr.substring(0, 5);
			}
			packet.setScore(tScoreStr);
		}
		return packet;
	}

	public Date getDate() {
		return this._date;
	}

	public void setDate(Date d) {
		this._date = d;
	}

	/**
	 * create an EXACT clone of 'this' post.
	 */
	public Post clone() {
		Post tCopy = new Post();
		tCopy._Content = this._Content;
		tCopy._date = this._date;
		tCopy._father = this._father;
		tCopy._id = this._id;
		tCopy._postId = this._postId;
		tCopy._name = this._name;
		tCopy._owner = this._owner;
		tCopy._type = this._type;
		return tCopy;
	}

}
